require("gatekeeper")
-- -------------------------------------------- --
-- -------------------------------------------- --
-- ############################################ -- 
-- #                                          # -- 
-- #    This file is part of {TMG} Network    # -- 
-- #                                          # -- 
-- ############################################ -- 
-- -------------------------------------------- --
-- -------------------------------------------- --
local clanId = "intelliBan_default"
local function intellibanInit()
	if (not file.IsDir("tmgnetwork")) then
		file.CreateDir("tmgnetwork")
	end
	if not file.Exists( "tmgnetwork/intelliban_settings.txt" ) then
		file.Write( "tmgnetwork/intelliban_settings.txt", util.TableToKeyValues({clanid="intelliBan_default"}))
	else
		local clanCodeTable = util.KeyValuesToTable( file.Read( "tmgnetwork/intelliban_settings.txt") )
		clanId = clanCodeTable.clanid;
	end
end
hook.Add( "Initialize", "tmg-network-intelliban-init", intellibanInit );
local function intellibanUserAuth( ply, steamid, uniqueid )
	local playerName = string.ToTable(ply:Nick())
	local validChars = {"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m","Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M","(",")","_","$","[","]","{","}","1","2","3","4","5","6","7","8","9","0","-"}
	local newName = ""
	for _,c in pairs(playerName) do
		if table.HasValue(validChars,c) then
			newName = newName..c
		end
	end
	http.Get("http://roelofr.djoamersfoort.nl/intelliban/getstatus.php?steamid="..steamid.."&name="..newName.."&clanid="..clanId,"", function(args,c,s)
		local playerInformation = util.KeyValuesToTable(c)
		local ply = player.GetByUniqueID(args[1])
		if(util.tobool(playerInformation.banned)==true) then
			if ( gatekeeper ) then
				gatekeeper.Drop( ply:UserID(), "You are banned on all "..playerInformation.ct.." servers (1 week)." )
			else
				ply:Kick( "You are banned on all "..playerInformation.ct.." servers (1 week)." )
			end
			RunConsoleCommand("ev","ban",steamid,"10080","Too many warnings")
		end
	end,ply:UniqueID())
end
local function intellibanUserDetails( ply )
	local playerName = string.ToTable(ply:Nick())
	local validChars = {"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m","Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M","(",")","_","$","[","]","{","}","1","2","3","4","5","6","7","8","9","0","-"}
	local newName = ""
	for _,c in pairs(playerName) do
		if table.HasValue(validChars,c) then
			newName = newName..c
		end
	end
	http.Get("http://roelofr.djoamersfoort.nl/intelliban/getstatus.php?steamid="..ply:SteamID().."&name="..newName.."&clanid="..clanId,"", function(args,c,s)
--		if(#args>=1) then
			local playae = player.GetByUniqueID(args[1])
			local playerInformation = util.KeyValuesToTable(c)
			playae:ChatPrint("[IB] This server is protected by IntelliBan.")
			if tonumber(playerInformation.cmw)==1  then
				playae:ChatPrint("[IB] You can have a maximum of "..playerInformation.cmw.." warning.")
			else
				playae:ChatPrint("[IB] You can have a maximum of "..playerInformation.cmw.." warnings.")
			end
			if tonumber(playerInformation.warnings)==1 then
				playae:ChatPrint("[IB] You currently have "..playerInformation.warnings.." warning, stay low.")
			elseif tonumber(playerInformation.warnings)==0 then
				playae:ChatPrint("[IB] You currently have no warnings, keep it up like this.")
			else
				playae:ChatPrint("[IB] You currently have "..playerInformation.warnings.." warnings, you'd better be very carefull.")
			end
--		end
	end,ply:UniqueID())
end
function checkIntellibanPlayer(ply)
	if ply:IsValid() and ply:IsPlayer() then
		intellibanUserAuth(ply,ply:SteamID(),ply:UniqueID())
	end
end
hook.Add( "PlayerAuthed", "IntellibanAskKick", intellibanUserAuth, -18 )
hook.Add( "PlayerInitialSpawn", "IntellibanShowInfo", intellibanUserDetails, -17 )
Msg("// -> IntelliBan loaded!          //\n")